﻿/*======================================*\
* Author: Vincent
* LastModifyUser: Vincent
* Description:  
* CreateTime： 2012-05
* Remark:  
\*======================================*/
using System;
using System.Drawing;
using System.Windows.Forms;
using AppCode.Domain.Services;
using System.Collections.Generic;

namespace Vincent.AutoCode 
{
    /// <summary>
    /// 主窗体类分部
    /// <remarks>
    /// 与tree操作相关的分部
    /// </remarks>
    /// </summary>
    public partial class frmMain
    {
        /// <summary>
        /// 数据库根节点
        /// </summary>
        TreeNode rootNode = null;

        /// <summary>
        /// 当前被选中的表
        /// </summary>
        List<string> currentSelectTable = null;
      
        /// <summary>
        /// 初始化树
        /// </summary>
        void TreeInfoOnInit()
        {
            treeView1.Nodes.Clear();
            rootNode = new TreeNode();
            rootNode.Text = string.Format("数据库:{0}", DbInfoInit.DbInfos.DbConnStrInfo.DbName);
            if (treeView1.InvokeRequired)
            {
                Action<TreeNode> ac = (a) =>
                {
                    treeView1.Nodes.Add(a);
                };
                treeView1.BeginInvoke(ac, new object[] { rootNode });
            }
            else
            {
                treeView1.Nodes.Add(rootNode);
                treeView1.ExpandAll();
            }

        }

        /// <summary>
        /// 加载表到树
        /// <remarks>
        /// node默认使用主线程创建
        /// </remarks>
        /// </summary>
        /// <param name="name"></param>
        void LoadTableToTree(string value,string text)
        {
            if (treeView1.InvokeRequired)
            {
                Action<string, string> ac = (nValue, nText) =>
                {
                    rootNode.Nodes.Add(nValue, nText);
                };
                treeView1.BeginInvoke(ac, new object[] { value,text });
            }
            else
            {
                rootNode.Nodes.Add(value,text);
            }
        }

        #region 树选中事件

        /// <summary>
        /// 树选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            TreeNode node = treeView1.GetNodeAt(new Point(e.X, e.Y));
            if (node != null)
            {
                ChangeChild(node, node.Checked);//影响子节点
                ChangeParent(node);//影响父节点
            }
        }

        /// <summary>
        /// 递归子节点跟随其全选或全不选
        /// </summary>
        /// <param name="node"></param>
        /// <param name="state"></param>
        private void ChangeChild(TreeNode node, bool state)
        {
            node.Checked = state;
            foreach (TreeNode tn in node.Nodes)
                ChangeChild(tn, state);
        }

        /// <summary>
        /// 递归父节点跟随其全选或全不选
        /// </summary>
        /// <param name="node"></param>
        private void ChangeParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                //兄弟节点被选中的个数
                int brotherNodeCheckedCount = 0;
                //遍历该节点的兄弟节点
                foreach (TreeNode tn in node.Parent.Nodes)
                {
                    if (tn.Checked == true)
                        brotherNodeCheckedCount++;
                }
                //兄弟节点全没选，其父节点也不选
                if (brotherNodeCheckedCount == 0)
                {
                    node.Parent.Checked = false;
                    ChangeParent(node.Parent);
                }
                //兄弟节点只要有一个被选，其父节点也被选
                if (brotherNodeCheckedCount >= 1)
                {
                    node.Parent.Checked = true;
                    ChangeParent(node.Parent);
                }
            }
        }

        #endregion

        #region 取得被选中的表

        void GetSelectTable()
        {
            if (rootNode != null)
            {
                currentSelectTable = new List<string>();
                foreach (TreeNode item in rootNode.Nodes)
                {
                    if (item.Checked && item.Index>=0)
                        currentSelectTable.Add(item.Name);
                }
            }
        }

        #endregion

    }
}
